package com.example.lvsuo.controller;


import com.example.lvsuo.Execl.Execldaoru.TaskService;
import com.example.lvsuo.service.ISystemRoleService;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.lvsuo.entity.SystemRole;
import com.example.lvsuo.entity.vo.SystemRoleVo;
import com.example.lvsuo.modal.Result;
import com.example.lvsuo.service.ISystemRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
/**
 * <p>
 * 系统角色表，给用户分配的角色 前端控制器
 * </p>
 *
 * @author 心心念念
 * @since 2021-03-30
 */
@RestController
@RequestMapping ("/systemRole")
public class SystemRoleController {
    // 改  调用 需要使用的service-----------------
    @Autowired
    ISystemRoleService ts;
    //----------改  路径
    @PostMapping("/list")
    public String analysisExcel(MultipartFile file) throws IOException {

        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            String fileName = file.getOriginalFilename();

            Workbook workbook = null;
            String fileType = fileName.substring(fileName.lastIndexOf("."));
            if (".xls".equals(fileType)) {
                workbook = new HSSFWorkbook(inputStream);
            } else if (".xlsx".equals(fileType)) {
                workbook = new XSSFWorkbook(inputStream);
            }

            ArrayList<ArrayList<Object>> list = new ArrayList<>();

            Sheet sheet;  Row row;  Cell cell;
            for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
                sheet = workbook.getSheetAt(i);
                for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
                    row = sheet.getRow(j);
                    if (row != null && row.getFirstCellNum() != j) {
                        ArrayList tempList = new ArrayList();
                        for (int k = row.getFirstCellNum(); k < row.getLastCellNum(); k++) {
                            cell = row.getCell(k);
                            tempList.add(cell.getStringCellValue());
                        }
                        list.add(tempList);
                    }
                }
            }
            //改 调用service的方法-----------------参数必须写list
            ts.lists(list);
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            inputStream.close();
        }
        return "success";
    }

    @Autowired
    ISystemRoleService iSystemRoleService;

    /**
     * 查询
     * @return
     */
    @GetMapping
    public Result getSystemRole(SystemRole systemRole){
        PageHelper.startPage(systemRole.getPageId(),systemRole.getPageNum());
        PageInfo pageInfo = new PageInfo(iSystemRoleService.getSystemRole(systemRole));
        return Result.back("ok", "200", pageInfo);
    }

    /**
     * 查询链表
     * @return
     */
    @GetMapping(params = {"chaxun=2"})
    public Result getSystemRua(){
     List<SystemRole> list=iSystemRoleService.getSystemRua();
        return Result.back("ok","200",list);
    }

    /**
     * 根据id查询
     * @return
     */
    @GetMapping(params = {"chaxun=1"})
    public Result getSystemRoleId(String id){
        SystemRoleVo list=iSystemRoleService.getSystemRoleId(id);
        return Result.back("ok","200",list);
    }

    /**
     * 添加
     * @param sys
     * @return
     */
    @PostMapping()
    public Result insertRoleOrAuthority(@RequestBody SystemRoleVo sys){
        iSystemRoleService.insertRoleOrAuthority(sys);
        return Result.back("ok","200");
    }

    /**
     * 修改
     * @param systemRoleVo
     * @return
     */
    @PutMapping
    public Result updateRoleOrAuthority(@RequestBody SystemRoleVo systemRoleVo){
        iSystemRoleService.updateRoleOrAuthority(systemRoleVo);
        return Result.back("ok","200");
    }

    /**
     * 删除
     * @param
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteRoleOrAuthority(@PathVariable String id){
        iSystemRoleService.deleteRoleOrAuthority(id);
        return Result.back("ok","200");
    }


}

